<?php
class category_index extends MX_Controller{
	public $category;
	
	public function listTableCategory( $currentcat = 0,  $parent = 0, $level = 0,$filter=''){
		$categories = $this->model()->get_categories( $parent,$filter );
		
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$pad = str_repeat( '&#8211; ', $level );
				$current= ( $currentcat == $cat->id )?1:0;
				
				$catRow=array("id"=>$cat->id,
										"parent"=>$cat->parent,
										"name"=>"$pad{$cat->name}",
										"short-name"=>$cat->name,
										"slug"=>$cat->slug,
										"link"=>$cat->link,
										"option"=>$cat->option,
										"description"=>$cat->description,
										"order"=>$cat->order,
										"active"=>$cat->active,
										"count"=>$cat->count,
										"current"=>$current);
				if($cat->option)$catRow['option']=$this->category_models_index->get_option($cat->option);
				$this->category[$cat->id]=$catRow;
				self::listTableCategory( $currentcat, $cat->id, $level +1,$filter );
			}
		}
	
	}
	
	public function dropdown_option_cats( $currentcat = 0,  $parent = 0, $level = 0, $category = '' ) {
		$categories = $this->model('category_models_index')->get_categories( $parent );
	
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$pad = str_repeat( '&#8211; ', $level );
				$class=!$level?' class="parent"':'';
				$s= ( $currentcat == $cat->id )?" selected='selected'":'';
				$catOption=  "<option value='{$cat->id}' $class $s >$pad{$cat->name}</option>\n\t";
				$category .=self::dropdown_option_cats( $currentcat, $cat->id, $level +1, $catOption );
			}
		}
		return $category;
	}
	public function dropdown_option_annonce_cats( $currentcat = 0,  $parent = 0, $level = 0, $category = '' ) {
		$categories = $this->model('category_models_index')->get_categories( $parent );
	
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$pad = str_repeat( '&#8211; ', $level );
				$class=!$level?' class="parent"':'';
				$value=$level? "value='{$cat->id}'":'';
				$s= ( $currentcat == $cat->id )?" selected='selected'":'';
				$catOption=  "<option $value $class $s >$pad{$cat->name}</option>\n\t";
				$category .=self::dropdown_option_cats( $currentcat, $cat->id, $level +1, $catOption );
			}
		}
		return $category;
	}
	public function check_list_cats( $currentcat = array(),  $parent = 0, $level = 0, $category = '',$href='' ) {
		
		$categories = $this->model()->get_categories( $parent );
		$catOption='';
		if ( is_array($categories) and !empty($categories) ) {
			if($level)$catOption.=  "\n<ul class='dropdown-menu'>\n";
			foreach ( $categories as $cat ) {
				//$pad = str_repeat( '&#8211; ', $level );
				$toggle=$caret=$classA=$classLI="";
				$toltal_row_sub_cat=$this->model()->get_total_categories(  $cat->id );
				if($toltal_row_sub_cat){
					$classLI=' class="dropdown" ';
					$classA='class="dropdown-toggle"  data-toggle="dropdown" ';
					$caret='<b class="caret"></b>';
					//$toggle=' data-toggle="dropdown" ';
				}
				$catOption.=  "
				<li $classLI >
				";
				$catOption.=  "<a $toggle href=\"$href{$cat->link}\" $classA id=\"{$cat->id}\" alt=\"{$cat->name}\"> {$cat->name} $caret</a>";
				$catOption .=self::check_list_cats( $currentcat, $cat->id, $level +1, $category,$href );
				$catOption .=  "
				</li>";
			}
	
			if($level)$catOption.=  "\n</ul>\n";
		}
			return $catOption;
	}
	
	public function check_list_cats_footer( $currentcat = array(),  $parent = 0, $level = 0, $category = '',$href='' ) {
	
		$categories = $this->model()->get_categories( $parent );
		$catOption='';
		if ( is_array($categories) and !empty($categories) ) {
			if($level)$catOption.=  "\n<ul class='unstyled'>\n";
			foreach ( $categories as $cat ) {
				//$pad = str_repeat( '&#8211; ', $level );
				$caret=$classA=$classLI="";
				$toltal_row_sub_cat=$this->model()->get_total_categories(  $cat->id );
				if($toltal_row_sub_cat){
					$classLI=' class="span2" ';
					$classA='class="sub-category"';
					//$caret='<span class="icon-category-'.$cat->slug.'"></span>';
				}
				$catOption.=  "
				<li $classLI >
				";
				$catOption.=  "<a href=\"$href{$cat->link}\" $classA  alt=\"{$cat->name}\" >$caret {$cat->name} </a>";
				$catOption .=self::check_list_cats_footer( $currentcat, $cat->id, $level +1, $category,$href );
				$catOption .=  "
				</li>";
			}
	
			if($level)$catOption.=  "\n</ul>\n";
		}
			return $catOption;
	}
	public function get_link_category($id_category){
		return $this->model()->get_link_category($id_category);
	}
	public function get_name_category($id_category){
		return $this->model()->get_name_category($id_category);
	}
	public function get_description_category($id_category){
		return $this->model()->get_description_category($id_category);
	}
	public function not_showPrice_category(){
		return $this->model()->not_showPrice();
	}
}